package com.example.myapplication.model.rx;

import android.content.Context;
import android.net.ParseException;

import com.example.myapplication.R;
import com.example.myapplication.app.App;
import com.example.myapplication.utils.ToastUtils;
import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.HttpException;
import retrofit2.Response;

/**
 * Created by meishiyiyun on 2017/4/24.
 */

public abstract class isThrowable<T> implements Callback<T> {
    private Context mContext;
    public isThrowable() {
        mContext= App.getInstance();
    }
    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        _onNext(response.body());

    }

    @Override
    public void onFailure(Call<T> call, Throwable e) {
        //请求异常提示
        String toast = "";
        String reason = "";
        if (e instanceof UnknownHostException)
            toast =App.getInstance().getString(R.string.network_connection_faile);
        else if (e instanceof SocketTimeoutException)
            toast = App.getInstance().getString(R.string.network_connection_time_out);
        else if (e instanceof ConnectException)
            toast = App.getInstance().getString(R.string.network_connection_time_out);
        else if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            int responseCode = httpException.code();
            if (responseCode >= 400 && responseCode <= 417) {
                toast = App.getInstance().getString(R.string.common_url_error);
            } else if (responseCode >= 500 && responseCode <= 505) {
                toast = App.getInstance().getString(R.string.network_connection_busy);
            } else {
                toast = App.getInstance().getString(R.string.network_connection_exception);
            }
        } else if (e instanceof ResultException) {
            ResultException resultException = (ResultException) e;
            int resultCode = resultException.getErrCode();
            if (resultCode == -1) {
//                GwtKeyApp.getInstance().doReLogin();
            } else if (resultCode <= 0) {
                reason = resultException.getMessage();
            } else {
                //正常不会进到这里
                toast = App.getInstance().getString(R.string.common_data_exception);
            }
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException
                || e instanceof NullPointerException)
            toast = App.getInstance().getString(R.string.common_data_exception);
        else
            toast = App.getInstance().getString(R.string.common_unknown_error);

        ToastUtils.showToast(toast);
        _onError();
    }

    protected abstract void _onNext(T t);

    protected abstract void _onError();
}
